package cn.edu.sptc.sms_weiwubo.controller;

import cn.edu.sptc.sms_weiwubo.entity.Student;
import cn.edu.sptc.sms_weiwubo.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/student")
@Tag(name = "学生管理",description = "学生的CRUD（增删查改）操作")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/query/{id}")
    @Operation(summary = "查询",description = "根据学生ID查询单个学生信息（路径变量形式）")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    @Parameters({
            @Parameter(description = "专业ID",required = true,example = "1",in = ParameterIn.PATH)
    })
    public Map<String,Object> queryMajorByID(@PathVariable String id){
        Map map = new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",studentService.queryByID(Long.parseLong(id)));
        return map;
    }
    @GetMapping("/query")
    @Parameters(
            @Parameter(name = "id", description = "学生ID",required = true,example = "1",in = ParameterIn.QUERY)
    )
    @Operation(summary = "查询",description = "根据学生ID查询单个学生信息（请求参数形式）")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    public Map<String,Object> queryMajorByIDParam(@RequestParam String id){
        Map map = new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",studentService.queryByID(Long.parseLong(id)));
        return map;
    }
    @GetMapping("/queryAll")
    @Operation(summary = "查询",description = "查询所有专业信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    public Map<String,Object> queryMajorByAll(){
        Map map = new HashMap();
        map.put("code","200");
        map.put("msg","成功");
        map.put("data",studentService.queryByAll());
        return map;
    }

    //    插入操作
    @PostMapping(value = "/save",consumes = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "插入",description = "通过JSON形式插入学生信息",operationId = "stuSaveByJSON")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "传入学生信息，学生ID、创建和更新时间不需要", required = true)
    public Map<String,Object> insertByJSON(@RequestBody Student student){
        Map map = new HashMap();
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());
        if(studentService.insertStudent(student)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }
    @PostMapping(value = "/save",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @Operation(summary = "插入",description = "通过表单形式插入学生信息",operationId = "stuSaveByForm")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "传入学生信息，学生ID、创建和更新时间不需要",required = true)
    public Map<String,Object> insertByForm(Student student){
        Map map = new HashMap();
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());
        if(studentService.insertStudent(student)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }

    //    更新操作
    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "更新",description = "通过JSON形式更新专业信息",operationId = "stuUpdateByJSON")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "传入学生信息（学生名称、性别、专业ID任选，学生ID必选）",required = true)
    public Map<String,Object> updateByJSON(@RequestBody Student student){
        Map map = new HashMap();
        student.setUpdateTime(new Date());
        if(studentService.updateStudent(student)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }
    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @Operation(summary = "更新",description = "通过表单形式更新专业信息",operationId = "stuUpdateByForm")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "传入学生信息（学生名称、性别、专业ID任选，学生ID必选）",required = true)
    public Map<String,Object> updateByForm(Student student){
        Map map = new HashMap();
        student.setUpdateTime(new Date());
        if(studentService.updateStudent(student)){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }

    @GetMapping("/delete/{id}")
    @Parameters(
            @Parameter(name = "id", description = "学生ID",required = true,example = "1",in = ParameterIn.PATH)
    )
    @Operation(summary = "删除",description = "根据学生ID删除学生信息（路径变量形式）")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    public Map<String,Object> deleteByID(@PathVariable String id){
        Map map = new HashMap();
        if(studentService.deleteStudent(Long.parseLong(id))){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }
    @GetMapping("/delete")
    @Parameters(
            @Parameter(name = "id", description = "学生ID",required = true,example = "1",in = ParameterIn.QUERY)
    )
    @Operation(summary = "删除",description = "根据学生ID删除学生信息（请求参数形式）")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "成功"),
            @ApiResponse(responseCode = "500",description = "服务器错误")
    })
    public Map<String,Object> deleteByIDParam(@RequestParam String id){
        Map map = new HashMap();
        if(studentService.deleteStudent(Long.parseLong(id))){
            map.put("code","200");
            map.put("msg","成功");
        }else {
            map.put("code","201");
            map.put("msg","失败");
        }
        return map;
    }

}
